<template>
  <div>
 
  <div v-if="show1" class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <div style="display: flex;">
      <el-form-item label="订单信息" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="手机号，订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      

      
      <div style="margin-left: 70px;">
        <el-form-item label="订单状态" prop="ordStatus">
        <el-select v-model="queryParams.ordStatus" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in field101Options" 
            :key="index" 
            :label="item.label"
            :value="item.value" 
            :disabled="item.disabled"
            @keyup.enter.native="handleQuery"
            >
          </el-option>
        </el-select>
      </el-form-item>
      </div>

      <div style="margin-left: 70px;">
      <el-form-item label="订单时间" prop="ordOrderTime">
        <el-date-picker clearable
          v-model="queryParams.ordOrderTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始时间">
        </el-date-picker>
      </el-form-item>

        <el-form-item  prop="endOrderTime">
        <el-date-picker clearable
          v-model="queryParams.endOrderTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      </div>
      </div>
      <el-form-item style="margin-left: 70px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['carService:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
      
    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />  
      <el-table-column label="订单编号" align="center" prop="ordNumber" />
      <el-table-column label="服务编号" align="center" prop="carServiceDetail.serNum" />
      <el-table-column label="服务名称" align="center">
        <template #default="{ row }">
        {{ row.carServiceDetail.proName }} : {{ row.carServiceType.serSort}}
      </template>
      </el-table-column>

      <el-table-column label="买家" align="center">
        <template #default="{ row }">
          <div>{{ row.carUser.nickName }}</div>
          <div>{{ row.carUser.userPhone}}</div>
      </template>
      </el-table-column>
      <el-table-column label="商家" align="center" prop="carMerchant.merName" />
      <el-table-column label="价格" align="center" prop="newPrice" >
        <template #default="{ row }">
          <div>原价:{{ row.carServiceDetail.oldPrice }}</div>
          <div>优惠:{{ row.carServiceDetail.oldPrice-row.ordActualPrice}}</div>
          <div>实付:{{ row.ordActualPrice}}</div>
      </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="ordStatus" />
      <el-table-column label="下单时间" align="center" prop="ordOrderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ordOrderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['carService:orders:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"

    /> 
  </div>


  <div v-if="!show1">
    <h2>订单详情</h2>
    <hr>
    <div class="shop-info">
      <table>
        <tr>
          <th>订单编号</th>
          <td>{{ form.ordNumber }}</td>
          <th>服务编号</th>
          <td>{{ form.carServiceDetail.serNum }}</td>
        </tr>
        <tr>
          <th>核销码</th>
          <td>{{ form.ordVerificationCode }}</td>
          <th>商家名称</th>
          <td>{{ form.carMerchant.merName }}</td>
        </tr>
        <tr>
          <th>买家</th>
          <td>{{ form.carUser.nickName }}</td>
          <th>订单状态</th>
          <td>{{ form.ordStatus }}</td>
        </tr>
        <tr>
          <th>下单时间</th>
          <td colspan="3">{{ form.ordOrderTime }}</td>
        </tr>
        <tr>
          <th>原价</th>
          <td colspan="3">{{ form.carServiceDetail.oldPrice }}</td>
        </tr>
        <tr>
          <th>优惠</th>
          <td colspan="3">{{ form.carServiceDetail.oldPrice - form.ordActualPrice }}</td>
        </tr>
        <tr>
          <th>实付金额</th>
          <td colspan="3">{{ form.ordActualPrice }}</td>
        </tr>
        <tr>
          <th>服务内容</th>
          <td colspan="3">{{ form.carServiceDetail.detContent }}</td>
        </tr>
      </table>
    </div>
    <button @click="show1=!show1">返回上一级</button>

  </div>


</div>


</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/carService/orders";

export default {
  name: "Orders",
  data() {
    return {
      show1: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ordNumber: null,
        userPhone: null,
        serviceDetailId: null,
        userId: null,
        merchantId: null,
        couId: null,
        ordStatus: null,
        ordOrderTime: null,
        serSort:"专业洗车",
        endOrderTime:null,
        searchValue:null,
      },
      field101Options: [{
        "label": "待支付",
        "value": "待支付"
      }, {
        "label": "已退款",
        "value": "已退款"
      },
      {
        "label": "已取消",
        "value": "已取消"
      },
      {
        "label": "已完成",
        "value": "已完成"
      },
      {
        "label": "待核销",
        "value": "待核销"
      }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // goBack() {
    //   // 实现返回上一级的功能
    //   // 例如，使用 router 或 history API
    //   this.$router.go(-1);
    // },
     /** 详情按钮操作*/
     handleDetail(row) {
      this.show1 = !this.show1;
      this.reset();
      const ordId = row.ordId || this.ids
      getOrders(ordId).then(response => {
        this.form = response.data;
        this.open = true;
      });
    },

    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ordId: null,
        ordNumber: null,
        serviceDetailId: null,
        userId: null,
        merchantId: null,
        couId: null,
        ordActualPrice: null,
        ordStatus: null,
        ordOrderTime: null,
        endOrderTime:null,
        ordVerificationCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ordId = row.ordId || this.ids
      getOrders(ordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ordId != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ordIds = row.ordId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ordIds + '"的数据项？').then(function() {
        return delOrders(ordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('carService/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.shop-info table {
  width: 100%;
  border-collapse: collapse;
  margin: 0px 30px 10px 30px;
}

.shop-info th,
.shop-info td {
  border: 1px solid #ccc;
  padding: 8px;
  text-align: center;
}

.shop-info th {
  background-color: #f2f2f2;
}

.sound-scenes {
  display: flex;
  //flex-direction: column; /* 或者使用 row 如果你希望它们水平排列 */  
  align-items: center;
  gap: 20px;

  /* 在项目之间添加间距 */
  .scene {
    margin: 30px;
    text-align: center;
  }

  .scene-image {
    width: 200px;
    /* 或者你想要的任何宽度 */
    height: 150px;
    /* 保持图片的原始宽高比 */
  }

  .scene-label {
    margin-top: 10px;
  }
}
</style>